package com.example.administrator.wallet.view.activity.finds.kkbank.Detail;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.adapter.DetailsRechargeAdapter;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.DetailsRechargeBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.administrator.wallet.view.activity.asset.RecordActivity;
import com.google.gson.Gson;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class DetailsCashWithdrawalFragment extends Fragment{
    private ListView lv_cash_withdrawal;

    IRequestManager reqequestManager = RequestFactory.getReqequestManager();

    private List<DetailsRechargeBean.DataBeanX.DataBean> dataBeanList;
    public DetailsRechargeAdapter adapter;//DetailsCashWithdrawalAdapter
    private DetailsRechargeBean detailsRechargeBean;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_details_cash_withdrawal,container,false);

        lv_cash_withdrawal = (ListView) view.findViewById(R.id.lv_cash_withdrawal);

        initData();


        return view;
//        item    item_transaction_record   ExpenditureRecordActivity
    }
    public void initAdapter() {
        adapter = new DetailsRechargeAdapter(getActivity(), dataBeanList);
        lv_cash_withdrawal.setAdapter(adapter);
    }
    private void initData() {
//        iDatas = new ArrayList<>();
        dataBeanList = new ArrayList<>();
        String token = SharedPrefsUtil.getValue(getActivity(), "wallet", "token", null);
        reqequestManager.httpPost(Api.BaseUrl + Api.Bank_Recharge_Details, Api.getBankRechargeDetails(token, "","","","3","",""), new IRequestCallback() {
            @Override
            public void onSuccess(String Response) {
                Gson gson = new Gson();
                detailsRechargeBean = gson.fromJson(Response, DetailsRechargeBean.class);
                if (detailsRechargeBean.getCode() == 200) {
                    if (detailsRechargeBean.getData() != null) {
                        dataBeanList.addAll(detailsRechargeBean.getData().getData());
                        initAdapter();
                        OnClick();
//                        smart.finishRefresh();
                    } else {
                        initData();
                    }
                }
            }

            @Override
            public void onFailure(String error) {
//                smart.finishRefresh();
//                toast(error);
            }
        });
    }
    private void OnClick() {
        adapter.getClickPst(new DetailsRechargeAdapter.OnClick() {
            @Override
            public void Click(DetailsRechargeBean.DataBeanX.DataBean dataBean) {
                Intent intent = new Intent(getActivity(), RecordActivity.class);
                intent.putExtra("datas", (Serializable) dataBean);
                getActivity().startActivity(intent);
            }
        });

//        smart.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(RefreshLayout refreshlayout) {
//                if (refreshlayout.isRefreshing()) {
//                    initData();
//                }
//            }
//        });
//
//        smart.setOnLoadmoreListener(null);
//        smart.setLoadmoreFinished(true);
    }
    @Override
    public void onResume() {
        super.onResume();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
}
